package com.young.nyd.util;

import com.young.cms.model.AdministrativeRegion;
import com.young.cms.service.IAdministrativeRegionService;
import com.young.cms.util.CmsConstants;
import com.young.common.client.ums.UmsClientHelper;
import com.young.common.exception.BaseException;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.interfaces.log.annotation.Log;
import com.young.nyd.model.CollaboratMonitoring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/nyd/administrativeRegion")
public  class XzquUserController {

    @Resource(name="administrativeRegionService")
    IAdministrativeRegionService service;


    public String selectUserXZBM(){
        String result = null;
        com.young.interfaces.ums.model.User loginUser = UmsClientHelper.getLoginUser();//已登陆用户
        if(loginUser == null){
           result =  null;
        }else {

            String companyId = loginUser.getCompanyId();
            Pattern pattern = Pattern.compile("[0-9]*");
            boolean isNumber = pattern.matcher(companyId).matches();
            if (companyId.equals("ROOT")) {
                result = "root";
            } else if (companyId.length() == 6 && isNumber) {
                if (companyId.substring(2).equals("0000")) {//省
                    result = companyId.substring(0, 3);
                } else if (companyId.substring(4).equals("00")) {//市
                    result = companyId.substring(0, 4);
                } else {//县
                    result = companyId;
                }
            }else{
                result =  companyId;
            }

        }
        return result;
    }



    /**
     * 查询省份列表
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询省份列表")
    @RequestMapping("/getProvinceListByuser")
    @ResponseBody
    public Object getProvinceList(HttpServletRequest request) throws Exception{
        com.young.interfaces.ums.model.User loginUser = UmsClientHelper.getLoginUser();//已登陆用户
        String companyid = loginUser.getCompanyId();
        if (loginUser == null){
            return  Result.build(Result.CodeEnum.SESSION_TIMEOUT);
        }else{
            AdministrativeRegion administrativeRegion = service.get(companyid);
            List<AdministrativeRegion> list = new ArrayList<AdministrativeRegion>();
            if(administrativeRegion == null){
                if(companyid.equals("ROOT")){
                    list = service.getProvinceList();
                }
            }else{
                Integer level = administrativeRegion.getAdminLevel();
                if(level == 1){//省
                    list.add(administrativeRegion);
                }else if(level == 2){//市
                    AdministrativeRegion pro = service.get(administrativeRegion.getParentId());
                    list.add(pro);
                }else if(level == 3){//县
                    AdministrativeRegion city = service.get(administrativeRegion.getParentId());
                    AdministrativeRegion pro = service.get(city.getParentId());
                    list.add(pro);
                }
            }
            return new Result(list);
        }

    }

    /**
     * 查询省份管辖的市列表
     * @param provinceCode 省份编码
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询省份管辖的市列表")
    @RequestMapping("/getCityListByProvinceCodeByuser")
    @ResponseBody
    public Object getCityListByProvinceCode(String provinceCode, HttpServletRequest request) throws Exception{
        com.young.interfaces.ums.model.User loginUser = UmsClientHelper.getLoginUser();//已登陆用户
        String companyid = loginUser.getCompanyId();
        if (loginUser == null){
            return  Result.build(Result.CodeEnum.SESSION_TIMEOUT);
        }else{
            AdministrativeRegion administrativeRegion = service.get(companyid);
            List<AdministrativeRegion> list = new ArrayList<AdministrativeRegion>();
            if(administrativeRegion == null){
                if(companyid.equals("ROOT")){
                    list = service.getCityListByProvinceCode(provinceCode);
                }
            }else{
                Integer level = administrativeRegion.getAdminLevel();
                if(level == 1){//省
                    list = service.getCityListByProvinceCode(provinceCode);
                }else if(level == 2){//市
                    list.add(administrativeRegion);
                }else if(level == 3){//县
                    AdministrativeRegion city = service.get(administrativeRegion.getParentId());
                    list.add(city);
                }
            }
            return new Result(list);
        }
//        List<AdministrativeRegion> list = service.getCityListByProvinceCode(provinceCode);

    }

    /**
     * 查询市管辖的县列表
     * @param cityCode 市编码
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询市管辖的县列表")
    @RequestMapping("/getCountyListByCityCodeByuser")
    @ResponseBody
    public Object getCountyListByCityCode(String cityCode, HttpServletRequest request) throws Exception{
        com.young.interfaces.ums.model.User loginUser = UmsClientHelper.getLoginUser();//已登陆用户
        String companyid = loginUser.getCompanyId();
        if (loginUser == null){
            return  Result.build(Result.CodeEnum.SESSION_TIMEOUT);
        }else{
            AdministrativeRegion administrativeRegion = service.get(companyid);
            List<AdministrativeRegion> list = new ArrayList<AdministrativeRegion>();
            if(administrativeRegion == null){
                if(companyid.equals("ROOT")){
                   list =  service.getCountyListByCityCode(cityCode);
                }
            }else{
                Integer level = administrativeRegion.getAdminLevel();
                if(level == 1){//省
                    list =  service.getCountyListByCityCode(cityCode);
                }else if(level == 2){//市
                    list =  service.getCountyListByCityCode(cityCode);
                }else if(level == 3){//县
                    list.add(administrativeRegion);
                }
            }
            return new Result(list);
        }
//        List<AdministrativeRegion> list = service.getCountyListByCityCode(cityCode);
//        return new Result(list);
    }


}
